<?php 
$page_title='View URLs';
include './includes/header.html';
require_once 'mysql_connect.php';
echo '<div align="center">
<form menthod="get" action="view_urls.php">
<select name="type">
<option value="NULL">Choose a Category</option>
';

$query='select * from url_categories order by category ASC';
$result=@mysql_query($query);
while ($row=mysql_fetch_array($result)){
	echo "<option value=\"$row[0]\">$row[1]</option>";
}

echo '</select><input type="submit" name="submit" value="GO!"></form></div>';

//if is selected make sure is an integer
if (isset($_GET['type'])){
	$type=(int)$_GET['type'];
}else{
	$type=0;
}

if ($type>0){
	$query="SELECT category FROM url_categories WHERE url_category_id =$type";
	$result=mysql_query($query);
	list($category)=mysql_fetch_array($result);
	
	echo "<hr/><div align=\"center\" ><b>$category Links</b></br>
	<small>(All links will open in their own window.Recently added links are listed first)</small></div>\n ";
	
	$first=TRUE;//initialize the variable
	
	$query="SELECT u.url_id, url, title, description
	FROM urls AS u, url_associations AS ua
	WHERE u.url_id = ua.url_id
	AND ua.url_category_id =$type
	AND ua.approved =  'Y'
	ORDER BY date_submitted DESC";
	$result=mysql_query($query);
	
	//display all urls
	while ($row=mysql_fetch_array($result,MYSQL_ASSOC)){
		if ($first){
			echo '<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
			<tr>
				<td align="right" width="40%"><font size="+1">Link</font></td>
				<td align="left" width="50%"><font size="+1">Description</font></td>
				<td align="center" width="10%">&nbsp</td>
			</tr>';
			$first=FALSE;
		}
		
		//display each record
		echo "<tr>
		<td align=\"right\"><a href=\"http://{$row['url']}\"target=\"_new\">{$row['title']}</a></td>
		<td align=\"left\">{$row['description']}</td>
		<td align=\"center\"><a href=\"edit_url.php?uid={$row['url_id']}\">edit</a></td>
		</tr>\n";
	}
	
	//if no record were displayed
	if ($first){
		echo '<div align="center">There are currently no links in this category.</div> ';
	}else{
		echo '</table>';
	}
}
mysql_close();
include './includes/footer.html';

?>